package com.hmall.search.web;

import com.hmall.common.PageDto;
import com.hmall.search.dto.SearchDto;
import com.hmall.search.pojo.ItemDoc;
import com.hmall.search.service.ISearchService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("search")
public class SearchController {

    @Autowired
    private ISearchService searchService;
    @Resource
    private RestHighLevelClient restHighLevelClient;
    /**
     * 1. 条件搜索
     *
     * @param  params 条件
     * @return 结果
     */
    @PostMapping("/list")
    @SneakyThrows
    public PageDto<ItemDoc> search(@RequestBody SearchDto params)
    {
        log.info("查询{}", params);
        return searchService.search(params);
    }

    /**
     * 2. 获取筛选条件
     *
     * @param params 请求参数对象
     * @return
     */
    @SneakyThrows
    @PostMapping("/filters")
    public Map<String, List<String>> filters(SearchDto params)
    {
        log.info("过滤筛选{}", params);
        return searchService.getFilters(params);
    }

    /**
     * 3. 关键词推荐
     *
     * @param  key 关键词前缀
     * @return 关键词集合
     */
        @GetMapping("/suggestion")
        @SneakyThrows
        public List<String> suggestion(String key)
        {
            log.info("字段补全{}", key);
            return searchService.getSuggestion(key);
        }

}
